from pathlib import Path

def search_and_replace_string(root_path, search_string, replace_string=None):
    root_path = Path(root_path)
    for item in root_path.iterdir():
        if item.is_file():
            try:
                with item.open() as file:
                    lines = file.readlines()
                with item.open('w') as file:
                    for line in lines:
                        if search_string in line:
                            if not replace_string:
                                print(f'在文件 {item} 中找到字符串 "{search_string}"')
                            else:
                                line = line.replace(search_string, replace_string)
                                print(f'在文件 {item} 中找到字符串 "{search_string}"，已替换为 "{replace_string}"')
                        file.write(line)
            except Exception as e:
                pass
        elif item.is_dir():
            search_and_replace_string(item, search_string, replace_string)

# 指定要搜索的根文件夹路径和要查找的字符串
root_folder = '/home/yusongli/Documents/yizhenrenxin/contrib/MedicalSeg/'

search_string = '食道肿瘤辅助医疗服务一体化平台'
# search_string = '开始使用'
# search_string = 'wqj.eb1a2fc.png'
# search_string = 'YU-TransNet'
# search_string = '后处理文件上传成功'
# search_string = 'image3dshow'
# search_string = '文件详情'

# replace_string = '食道肿瘤辅助医疗服务一体化平台'
# replace_string = 'YU-TransNet'
# replace_string = '食道肿瘤辅助医疗服务一体化平台'
# replace_string = None
replace_string = '食道肿瘤辅助医疗服务一体化平台'

# 食道肿瘤辅助医疗服务一体化平台
# 食道癌辅助诊疗系统设计与实现
# 基于深度学习的食道癌辅助诊断系统

search_and_replace_string(root_folder, search_string, replace_string)
